//
//  SDRequest.m
//  Sodom
//
//  Created by 赵隆杰 on 2019/12/8.
//  Copyright © 2019 赵隆杰. All rights reserved.
//

#import "ZCRequest.h"
#import "NSObject+Common.h"

@implementation ZCRequest

-(BOOL)success
{
    return [[[self responseJSONObject] objectForKey:@"code"] intValue] == 200;
}
-(NSString * )msg
{
    return [[self responseJSONObject] objectForKey:@"msg"];
}
-(id)res
{
    return  [[self responseJSONObject] objectForKey:@"data"];
}
-(id)result
{
    return  [self responseJSONObject];
}

#pragma mark 添加请求头
-(NSDictionary *)requestHeaderFieldValueDictionary
{
    // 本地的cookie， 上次的cookie
//    NSString *localToken = [SDLocalUser fetchToken];
    NSMutableDictionary *headParams=[[NSMutableDictionary alloc]init];
//    if (localToken.length) {
//       [headParams setObject:localToken forKey:@"token"];
//    }
//    [headParams setObject:@"mbk8ng8agprbonxp5m196" forKey:@"appkey"];
//    NSLog(@"headParams:%@", headParams);
    return headParams;
}

- (void)requestCompleteFilter {
    [self hideHUDView];
    // 本地的cookie， 上次的cookie
//    NSString *localToken = [SDLocalUser fetchToken];
    // 这次请求反的cookie，有可能没有的，接口只会在需要更新cookie的时候返
//    NSString *responseToken = [[self.response allHeaderFields] valueForKey:@"Set-Cookie"];
//
//    if ([responseToken isKindOfClass:[NSString class]]) {
//        if (responseToken.length && [responseToken hasPrefix:@"token="]) {
//
//            NSArray *setCookieAry = [responseToken componentsSeparatedByString:@";"];
//            if (setCookieAry.count) {
//                NSString *newToken = setCookieAry[0];
//                if (newToken.length > 6 && ![localToken isEqualToString:newToken]) {
//                    [SDLocalUser saveToken:newToken];
//                }
//            } else {
//                if (![localToken isEqualToString:responseToken]) {
//                    [SDLocalUser saveToken:responseToken];
//                }
//            }
//
//        }
//    }
//    else if ([responseToken isKindOfClass:[NSArray class]]) {
//        NSArray *responseTokenAry = (NSArray *)responseToken;
//        for (NSString *_resToken in responseTokenAry) {
//            if (_resToken.length && [_resToken hasPrefix:@"token="]) {
//
//                NSArray *setCookieAry = [_resToken componentsSeparatedByString:@";"];
//                if (setCookieAry.count) {
//                    NSString *newToken = setCookieAry[0];
//                    if (newToken.length > 6 && ![localToken isEqualToString:newToken]) {
//                        [SDLocalUser saveToken:newToken];
//                    }
//                } else {
//                    if (_resToken.length > 6 && ![localToken isEqualToString:_resToken]) {
//                        [SDLocalUser saveToken:_resToken];
//                    }
//                }
//                break;
//            }
//        }
//    }
    
    
        
    NSLog(@"requestUrl:%@\nrequestArgument:%@", self.requestUrl,self.requestArgument);

    if (self.success) {
//        NSLog(@"res---\n%@", self.res);
    } else {
        NSDictionary *errDic = self.responseJSONObject;
        NSLog(@"code:%@\nmsg:%@", errDic[@"code"], errDic[@"msg"]);
        self.errorCode = [errDic[@"code"] intValue];
        if ([errDic[@"code"] intValue] == 401) {
            //未登录
//            SLWeakSelf
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"数据失效，需要重新登录" preferredStyle:UIAlertControllerStyleAlert];
            [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                SLStrongSelf
//                [SDLocalUser clearLoginInfo];
//                [PushScheduler rootControllerSwitchToLoginVC];
            }]];
            [[UIWindow currentViewController] presentViewController:alertController animated:YES completion:nil];
            
            return;
        }
        if (![NSObject isBlankString:errDic[@"msg"]]) {
            [[UIWindow currentViewController].view makeToast:errDic[@"msg"]];
        }
        
    }
    
}

- (void)requestFailedFilter
{
    [self hideHUDView];
//    NSLog(@"requestUrl:%@\nrequestArgument:%@\nresponseJSONObject:%@", self.requestUrl, self.requestArgument, self.responseJSONObject);
    if (self.response.statusCode == 500) {
        [UIDevice.vg_rootWindow makeToast:@"服务器错误" duration:1.0 position:CSToastPositionCenter];
        return;
    }
}

- (void)start
{
    [super start];
    [self showHUDView];
}

- (void)showHUDView
{
    if (!self.hideHUD) {
        UIView *hudView = nil;
        if (self.HUDView) {
            hudView = self.HUDView;
        } else if ([UIWindow currentViewController].view) {
            hudView = [UIWindow currentViewController].view;
        }
        if (hudView) {
            if (![MBProgressHUD HUDForView:hudView]) {
                [MBProgressHUD showHUDAddedTo:hudView animated:YES];
            }
        }
    }
}
- (void)hideHUDView
{
    if (!self.hideHUD) {
        UIView *hudView = nil;
        if (self.HUDView) {
            hudView = self.HUDView;
        } else if ([UIWindow currentViewController].view) {
            hudView = [UIWindow currentViewController].view;
        }
        if (hudView) {
            if ([MBProgressHUD HUDForView:hudView]) {
                [MBProgressHUD hideHUDForView:hudView animated:YES];
            }
        }
    }
}


@end
